//将ajax()、post()请求时的数组参数视为一个数组,而不是多个数组参数
jQuery.ajaxSettings.traditional = true;

//日期格式化
Date.prototype.format = function (format) {
    var args = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var i in args) {
        var n = args[i];
        if (new RegExp("(" + i + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
    }
    return format;
};

/**通过JSON字符串向界面对象放值的方法
 * 例子
 * $("#form1").setFormValues({id:111,name:'ccc',dyh:'aaaa' ,khfs:'-1'});
 * auth sntey
 * @param {Object} $
 */
(function ($) {
    $.fn.setFormValues = function (json) {
        var form1 = $(this);
        $.each(json, function (id, value) {
            /**该功能是为jquery版的 combox提供**/
            var this_obj = form1.find("[name='" + id + "']");
            var this_obj_type = this_obj.attr("type");
            var  tagName=null;
            if(this_obj.length!=0){
                tagName =this_obj[0].tagName;
            }
            if (undefined == this_obj_type || this_obj_type == "" || "text" == this_obj_type) {
                this_obj.val(value);
            } else if (this_obj_type == "radio" || "checkbox" == this_obj_type) {
                if (value instanceof Array) {
                    this_obj.each(function () {
                        for (i = 0; i < value.length; i++) {
                            if ($(this).val() == value[i]) {
                                $(this).prop({'checked': true});
                            }
                        }
                    });
                } else {
                    this_obj.each(function () {
                        if ($(this).val() == value) {
                            $(this).prop({'checked': true});
                        } else {
                            $(this).prop({'checked': false});
                        }
                    });
                }
            } else {
                this_obj.val(value);
            }
            //进行select的赋值
            if( tagName == "SELECT"){
                if(value==null){
                    this_obj.val(" ").trigger('change');
                }else{
                    this_obj.val(value).trigger('change');
                }
            }
        });
    };
    $.fn.clearFormValues = function () {
        var form1 = $(this);
        form1.find("input").each(function () {
            var this_obj = $(this);
            var this_obj_type = $(this).attr("type");
            if (undefined == this_obj_type || this_obj_type == "" || "text" == this_obj_type || "hidden" == this_obj_type) {
                this_obj.val("");
            } else if (this_obj_type == "radio" || "checkbox" == this_obj_type) {
                this_obj.each(function () {
                    this_obj.prop({'checked': false});
                });
            } else {
                this_obj.val("");
            }
        });
        form1.find("select").each(function () {
            $(this).val("");
        });
        form1.find("textarea").each(function () {
            $(this).val("");
        });
    };
    $.fn.validate = function () {
        var form1 = $(this);
        b = true;
        $.each(form1.find(".must"), function (i, v) {
            var cv = $(this).val();
            if (cv.replace(/\s/g, "") == "") {
                $(this).focus();
                b = false;
                return b;
            }
        });

        return b;
    };
    /****
     * 判断对象的值是否为空.
     * @memberOf {TypeName}
     * @return {TypeName}
     */
    $.fn.isEmpty = function () {
        var cv = $(this).val();
        return cv.replace(/\s/g, "") == ""
    };
    //单选框的点击与取消点击
    //绑定点击事件

})(jQuery);

var enter_event={
    //回车事件,不做任何处理
    event_null:function () {
        $(document).keyup(function (e) {
            if (e.keyCode == 13) {
               return false
            }
        });
    },
    //回车事件,触发指定按钮的点击
    event_button_click:function (button_id) {
        document.onkeydown = function(e){
            var ev = document.all ? window.event : e;
            if(ev.keyCode==13) {
                $("#"+button_id).click();
                return false;
            }
        }
    },
    //回车事件,触发id为btn_save的按钮的点击事件
    event_save_click:function () {
        enter_event.event_button_click("btn_save")
    },
    //回车事件,触发id为btn_search的按钮的点击事件
    event_search_click:function () {
        enter_event.event_button_click("btn_search")
    }
}

var select2_utils={
    //在没有查询到数据时,使用的select2参数
    null_data:{
        language: 'zh-CN',
        placeholder:"没有查询到数据",
        data:null,
    },
    /*
    * 以select2加载一个下拉框
    * label_id              下拉框的id
    * placeholder           要显示的提示文本
    * url                   请求的url
    * para                  请求附带的参数
    * key1                  展示返回数据的那个字段
    * key2                  额外展示返回数据的那个字段
    * key_id                id使用返回数据的那个字段
    * null_select           空值选择项的显示文本,为null则没有空值选择项
    * */
    load_select2_key_id:function (label_id,placeholder,url,para,key1,key2,key_id,null_select) {
        var sele="#"+label_id;
        $.getJSON(url,para!=null?para:{},function(o){
            var data=o.data;
            var count=o.data.length;
            if(o.code ==0 &&data!=null&&count!=0){
                var select2_data=select2_utils.arrayToSelect2_key2_id(data,key_id,key1,key2,placeholder,null_select);
                $(sele).select2(select2_data);
            }else{
                $(sele).select2(select2_utils.null_data);
            }
        });
    },

    load_select2_key:function (label_id,placeholder,url,para,key1,key2) {
        return select2_utils.load_select2_key_id(label_id,placeholder,url,para,key1,key2,"id",null);
    },
    load_select2_key2:function (label_id,placeholder,url,para,key2) {
        return select2_utils.load_select2_key_id(label_id,placeholder,url,para,"name",key2,"id",null);
    },
    load_select2:function (label_id,placeholder,url,para) {
        return select2_utils.load_select2_key_id(label_id,placeholder,url,para,"name",null,"id",null);
    },



    /*
       快捷的将查询的数据转换为select2可用的数组,将转换为形如:张三(123456)  李四(计算机专业) 的数据形式
       data:要转换的源数据
       key_id: 要转换为id的 属性
       key1:要转换为name前缀的属性值
       key2:要转换为name括号里信息 的属性值
       placeholder:要展示的占位符信息
       null_value:空值选择项的文本
    * */
    arrayToSelect2_key2_id:function (data,key_id, key1, key2, placeholder,null_value) {
        var temp = [];
        if(null_value!=null){
            temp.push({id: " ", text: null_value});
        }
        $.each(data, function (index, elem) {
            var value=elem[key1];
            if(key2!=null){
                value=value + "(" + elem[key2] + ")";
            }
            temp.push({id: elem[key_id], text: value});
        })
        return {language: 'zh-CN',data: temp, placeholder: placeholder};
    },
    //快捷的将查询的数据转换为select2可用的数组,仅适用于简单的转换
    arrayToSelect2:function(data, key, placeholder) {
        return select2_utils.arrayToSelect2_key2_id(data,"id",key,null,placeholder)
    },
    //快捷的将查询的数据转换为select2可用的数组,仅适用于简单的转换
    arrayToSelect2_key:function(data, key1,key2, placeholder) {
        return select2_utils.arrayToSelect2_key2_id(data,"id",key1,key2,placeholder)
    }

}

var tool={
    isEmpty:function (object) {
        switch (typeof object) {
            case 'undefined':
                return true;
            case 'string':
                if (object.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
                break;
            case 'boolean':
                if (!object) return true;
                break;
            case 'number':
                if (0 === object || isNaN(object)) return true;
                break;
            case 'object':
                if (null === object || object.length === 0) return true;
                for (var i in object) {
                    return false;
                }
                return true;
        }
        return false;
    },
    //form的封装参数数组转为参数字面量
    paraArrayToLiteral:function (para) {
        var literal = {};
        for (var i = 0; i < para.length; i++) {
            var key=para[i].name;
            //如果当前值已经存在,说明有一个参数是数组
            if(literal[key]!=null){
                //把value改成数组类型,储存所有值
                var curr=literal[key];
                if((typeof curr)=="object"){
                    curr.push(para[i].value)
                    literal[key]=curr;
                }else{
                    var temp=new Array()
                    temp.push(curr);
                    temp.push(para[i].value)
                    literal[key]=temp;
                }
            }else{
                literal[key] = para[i].value
            }

        }
        return literal;
    },
    //form的封装参数数组转为参数字面量
    LiteralToString:function (literal) {
        var index = 0;
        var res = "";
        for (var key in literal) {
            if (index == 0) {
                res = res + "?";
            } else {
                res = res + "&";
            }
            res = res + key + "=" + literal[key];
            index++;
        }
        return res;
    },
    //将字面量数组封装成参数字符串
    ArrayLiteralToString:function (array) {
        var index = 0;
        var res = "";
        for (var i = 0; i < array.length; i++) {
            var literal = array[i];
            for (var key in literal) {
                if (index == 0) {
                    res = res + "?";
                } else {
                    res = res + "&";
                }
                res = res + key + "=" + literal[key];
                index++;
            }
        }
        return res;
    },
    //将数组封装成参数字符串
    ArrayToString:function (paraName,array) {
        var res = "";
        for (var i = 0; i < array.length; i++) {
            if (i == 0) {
                res = res + "?";
            } else {
                res = res + "&";
            }
            res = res + paraName + "=" + array[i];
        }
        return res;
    },
    //获取字面量数组的第一个id项
    paraGetOneId:function (para) {
        if (para.length == 0) {
            return null;
        } else {
            var temp = para[0];
            return temp.id;
        }
    },
    //获取字面量数组的id项
    paraGetId:function (para) {
        var ids = [];
        if (para.length == 0) {
            return null;
        }
        for (var i = 0; i < para.length; i++) {
            ids.push(para[i].id)
        }
        return ids;
    },

    //将数组转换为","分隔的字符串
    asString:function (para) {
        var ids = "";
        if (para.length == 0) {
            return null;
        }
        for (var i = 0; i < para.length; i++) {
            if(i!=0){
                ids=ids+",";
            }
            ids=ids+(para[i])
        }
        return ids;
    },
    //以json格式发送post请求.用于在后台有@requestBody注解时
    postJSON:function (url,para,success) {
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(para),
            dataType: "json",
            success:success
        });
    }

}

var common_utils={
    status_map:{
        "-1":["禁用","brown"],
        "0":["启用","forestgreen"]
    },
    transform_map:function (key,map) {
        var res=map[key];
        return res!=null?res:"";
    },
    transform_map_color:function (key,map) {
        var value=map[key];
        if(value==null){
            return "";
        }
        var html="<span style='color:"+value[1]+"'>"+value[0]+"</span>"
        return html;
    },
    status_transform:function (key) {
        return common_utils.transform_map_color(key,common_utils.status_map)
    }

}
